package com.ms.action.basic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eddy.util.AjaxOut;
import com.ms.dao.entity.basic.ResourcesEty;
import com.ms.dao.entity.basic.RoleResourceEty;
import com.ms.dao.mapper.basic.RoleResourceDao;

@Controller
@RequestMapping("/HTML/basic/RoleResourceAction")
public class RoleResourceAction {
	
	private static final long serialVersionUID = 6572194470077716769L;
	
	@Autowired
	private RoleResourceDao roleResourceDao;

	@RequestMapping("/getRoleResources.action")
	public void getRoleResources(HttpServletRequest request, HttpServletResponse response) {
		try {
			JSONArray array = new JSONArray();
			String node = request.getParameter("node");
			String roleIdStr = request.getParameter("roleId");
			if(roleIdStr == null || roleIdStr.trim().equals("")) {
				return;
			}
			RoleResourceEty ety = new RoleResourceEty();
			ety.setResourceId(Integer.parseInt(node));
			ety.setRoleId(Integer.parseInt(roleIdStr));
			List<ResourcesEty> list = roleResourceDao.selectRoleResources(ety);
			for(int i = 0; i < list.size(); i++) {
				ResourcesEty rsEty = list.get(i);
				JSONObject obj = new JSONObject();
				obj.put("id", rsEty.getNodeId());
				obj.put("text", rsEty.getMenuName());
				
				if(rsEty.getActionPath() == null || rsEty.getActionPath().trim().equals(""))
					obj.put("leaf", false);
				else
					obj.put("leaf", true);
				
				if(rsEty.getOpenIcon() != null && !rsEty.getOpenIcon().trim().equals(""))
					obj.put("checked", true);
				else
					obj.put("checked", false);
				
				array.add(obj);
			}
			AjaxOut.responseText(response, array.toString());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/saveRoleResources.action")
	public void saveRoleResources(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String roleId = request.getParameter("roleId");
			String selectNodes = request.getParameter("selectNodes");
			String allNodes = request.getParameter("allNodes");
			
			String[] allNodesArr = StringUtils.split(allNodes, ",");
			allNodes = StringUtils.join(allNodesArr, ",");
			Map<String, String> pMap = new HashMap<String, String>();
			pMap.put("roleId", roleId);
			pMap.put("allNodes", allNodes);
			roleResourceDao.deleteByIds(pMap);
			
			String[] rsNodes = StringUtils.split(selectNodes, ",");
			for(int i = 0; i < rsNodes.length; i++) {
				RoleResourceEty ety = new RoleResourceEty();
				ety.setRoleId(Integer.parseInt(roleId));
				ety.setResourceId(Integer.parseInt(rsNodes[i]));
				roleResourceDao.insert(ety);
			}
			
			obj.put("result","success");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		AjaxOut.responseText(response, obj.toString());
	}
}
